package com.ken.checkviolationapp.ui.news

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.ken.base_http_library.news.NewsResponse
import com.ken.base_library.ui.BaseFragment
import com.ken.checkviolationapp.R
import com.ken.checkviolationapp.adapter.MyRecyclerAdapter
import com.ken.checkviolationapp.adapter.NewsAdapter
import com.ken.checkviolationapp.bean.ANewsBean
import com.ken.checkviolationapp.bean.BNewsBean
import com.ken.checkviolationapp.bean.NewsTitle
import com.ken.checkviolationapp.bean.TypeInterf
import com.ken.checkviolationapp.databinding.FragmentNewContentBinding
import com.ken.rxhttp.callback.OnDesCallback
import com.ken.rxhttp.http_api_impl.ApiServiceImpl

/**
 * @author by ken
 * Blog:
 * Date:2020/5/13
 * Description:
 */
class NewsContentFragment(index: Int) : BaseFragment(), SwipeRefreshLayout.OnRefreshListener {
    val TAG = NewsContentFragment::class.java.name
    lateinit var newContentBinding: FragmentNewContentBinding
    private var mList: MutableList<TypeInterf> = arrayListOf()
    private var type: Int = 0
    //    lateinit var adapter: NewsAdapter<TypeInterf>
    lateinit var adapter: MyRecyclerAdapter

    private val mEnList = arrayOf(
        "top",
        "shehui",
        "guonei",
        "guoji",
        "yule",
        "tiyu",
        "junshi",
        "keji",
        "caijing",
        "shishang"
    )

    init {
        this.type = index
    }

    override fun initDataBind(inflater: LayoutInflater) {
        newContentBinding = FragmentNewContentBinding.inflate(inflater)
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        setAdapter()

    }

    override fun initListener() {
        newContentBinding.srlLoading.setOnRefreshListener(this)
    }

    override fun initData() {
        newContentBinding.ti = NewsTitle(mEnList[type])
        println("发起请求...")
        ApiServiceImpl.getNews(
            "d7f5ed752c6c9a438454ab9fa024148d",
            mEnList[type],
            object : OnDesCallback<NewsResponse> {
                override fun onSuccess(res: NewsResponse) {
                    for (new in res.data) {
                        if (new.thumbnail_pic_s02 == null) {
                            mList.add(ANewsBean(new))
                        } else {
                            mList.add(BNewsBean(new))
                        }
                    }

                    newContentBinding.srlLoading.isRefreshing = false
                    adapter.notifyDataSetChanged()
                }

                override fun onError(code: Int, msg: String) {
                    newContentBinding.srlLoading.isRefreshing = false
                    println("${TAG} ... code...${code},msg...${msg}")
                }
            })
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_new_content
    }

    override fun databing(): View {
        return newContentBinding.root
    }

    private fun setAdapter() {
        adapter = activity?.let { MyRecyclerAdapter(it, mList) }!!
        newContentBinding.rvList.layoutManager = LinearLayoutManager(context)
        newContentBinding.rvList.adapter = adapter

    }

    override fun onRefresh() {
        mList.clear()
        initData()
    }
}